package com.headline.controller;

import com.headline.pojo.User;
import com.headline.service.UserService;
import com.headline.utils.JwtHelper;
import com.headline.utils.Result;
import com.headline.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class userController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        Result result = userService.login(user);
        return result;
    }
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader String token){
        Result result = userService.getUserInfo(token);
        return result;
    }
    @PostMapping("/checkUserName")
    public Result checkUserName(@Param("username") String username){
        Result result = userService.checkUserName(username);
        return result;
    }
    @PostMapping("/regist")
    public Result regist(@RequestBody User user){
        Result result = userService.regist(user);
        return result;
    }
    /**
     * 检验用户登录是否有效
     * @return
     */
    @GetMapping("/checkLogin")
    public Result checkLogin(@RequestHeader String token){
        boolean jwtHelperExpiration = jwtHelper.isExpiration(token);
        if(jwtHelperExpiration || token == null){
            //如果token 为false说明没有过期，依旧可以进行操作
            //如果token为true 说明token过期了
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.OK(null);
    }

}
